package usbcali.edu.co.pqrs.presentation.backingBeans;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import usbcali.edu.co.pqrs.exceptions.ZMessManager;
import usbcali.edu.co.pqrs.modelo.MotSolSelect;
import usbcali.edu.co.pqrs.modelo.MotivoSolicitud;
import usbcali.edu.co.pqrs.modelo.SolicitudPqr;
import usbcali.edu.co.pqrs.modelo.dto.MotReclSelectDTO;
import usbcali.edu.co.pqrs.modelo.dto.MotSolSelectDTO;
import usbcali.edu.co.pqrs.modelo.dto.MotivoSolicitudDTO;
import usbcali.edu.co.pqrs.modelo.dto.SolicitudPqrDTO;
import usbcali.edu.co.pqrs.presentation.businessDelegate.IBusinessDelegatorView;
import usbcali.edu.co.pqrs.utilities.FacesUtils;

/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * 
 */
@ManagedBean
@ViewScoped
public class MotSolSelectView implements Serializable {
	private static final long serialVersionUID = 1L;

	private Long motivoSolicitud;
	private Long solicitudPqr;
	private SelectOneMenu txtSolicitudPqr;
	private SelectOneMenu txtMotivoSolicitud;
	private Map<String, String> losItemsSolicitud = new HashMap<String, String>();
	private Map<String, String> losItemsMotivoSolicitud = new HashMap<String, String>();
	private CommandButton btnBack;
	List<MotivoSolicitudDTO> data2;
	List<SolicitudPqrDTO> data3;

	private InputText txtIdMotSol_MotivoSolicitud;
	private InputText txtIdSolPqr_SolicitudPqr;
	private InputText txtIdMotSolSelected;
	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnClear;
	private List<MotSolSelectDTO> data;
	private MotSolSelectDTO selectedMotSolSelect;
	private MotSolSelect entity;
	private boolean showDialog;
	@ManagedProperty(value = "#{BusinessDelegatorView}")
	private IBusinessDelegatorView businessDelegatorView;

	public MotSolSelectView() {
		super();
	}

	public void rowEventListener(RowEditEvent e) {
		try {
			MotSolSelectDTO motSolSelectDTO = (MotSolSelectDTO) e.getObject();

			if (txtIdMotSol_MotivoSolicitud == null) {
				txtIdMotSol_MotivoSolicitud = new InputText();
			}

			txtIdMotSol_MotivoSolicitud.setValue(motSolSelectDTO
					.getIdMotSol_MotivoSolicitud());

			if (txtIdSolPqr_SolicitudPqr == null) {
				txtIdSolPqr_SolicitudPqr = new InputText();
			}

			txtIdSolPqr_SolicitudPqr.setValue(motSolSelectDTO
					.getIdSolPqr_SolicitudPqr());

			if (txtIdMotSolSelected == null) {
				txtIdMotSolSelected = new InputText();
			}

			txtIdMotSolSelected.setValue(motSolSelectDTO.getIdMotSolSelected());

			Long idMotSolSelected = FacesUtils.checkLong(txtIdMotSolSelected);
			entity = businessDelegatorView.getMotSolSelect(idMotSolSelected);

			action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_new() {
		action_clear();
		selectedMotSolSelect = null;
		setShowDialog(true);

		return "";
	}

	public String action_clear() {
		entity = null;
		selectedMotSolSelect = null;

		if (txtIdMotSol_MotivoSolicitud != null) {
			txtIdMotSol_MotivoSolicitud.setValue(null);
			txtIdMotSol_MotivoSolicitud.setDisabled(true);
		}

		if (txtIdSolPqr_SolicitudPqr != null) {
			txtIdSolPqr_SolicitudPqr.setValue(null);
			txtIdSolPqr_SolicitudPqr.setDisabled(true);
		}

		if (txtIdMotSolSelected != null) {
			txtIdMotSolSelected.setValue(null);
			txtIdMotSolSelected.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(true);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(true);
		}

		return "";
	}

	public String action_back() {

		return "initialMenu.xhtml?faces-redirect=true";
	}

	public void listener_txtId() {
		try {
			Long idMotSolSelected = FacesUtils.checkLong(txtIdMotSolSelected);
			entity = (idMotSolSelected != null) ? businessDelegatorView
					.getMotSolSelect(idMotSolSelected) : null;
		} catch (Exception e) {
			entity = null;
		}

		if (entity == null) {
			txtIdMotSol_MotivoSolicitud.setDisabled(false);
			txtIdSolPqr_SolicitudPqr.setDisabled(false);
			txtIdMotSolSelected.setDisabled(false);
			btnSave.setDisabled(false);
		} else {
			txtIdMotSol_MotivoSolicitud.setValue(entity.getMotivoSolicitud()
					.getIdMotSol());
			txtIdMotSol_MotivoSolicitud.setDisabled(false);
			txtIdSolPqr_SolicitudPqr.setValue(entity.getSolicitudPqr()
					.getIdSolPqr());
			txtIdSolPqr_SolicitudPqr.setDisabled(false);
			txtIdMotSolSelected.setValue(entity.getIdMotSolSelected());
			txtIdMotSolSelected.setDisabled(true);
			btnSave.setDisabled(false);

			if (btnDelete != null) {
				btnDelete.setDisabled(false);
			}
		}
	}

	public String action_edit(ActionEvent evt) {
		selectedMotSolSelect = (MotSolSelectDTO) (evt.getComponent()
				.getAttributes().get("selectedMotSolSelect"));
		txtIdMotSol_MotivoSolicitud.setValue(selectedMotSolSelect
				.getIdMotSol_MotivoSolicitud());
		txtIdMotSol_MotivoSolicitud.setDisabled(false);
		txtIdSolPqr_SolicitudPqr.setValue(selectedMotSolSelect
				.getIdSolPqr_SolicitudPqr());
		txtIdSolPqr_SolicitudPqr.setDisabled(false);
		txtIdMotSolSelected
				.setValue(selectedMotSolSelect.getIdMotSolSelected());
		txtIdMotSolSelected.setDisabled(true);
		btnSave.setDisabled(false);
		setShowDialog(true);

		return "";
	}

	public String action_save() {
		try {
			if ((selectedMotSolSelect == null) && (entity == null)) {
				action_create();
			} else {
				action_modify();
			}

			data = null;
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_create() {
		try {
			entity = new MotSolSelect();

			MotivoSolicitud motivoReclamacionItem = businessDelegatorView
					.getMotivoSolicitud(motivoSolicitud);
			SolicitudPqr solicitudPqrItem = businessDelegatorView
					.getSolicitudPqr(solicitudPqr);

			entity.setMotivoSolicitud(motivoReclamacionItem);

			entity.setSolicitudPqr(solicitudPqrItem);

			businessDelegatorView.saveMotSolSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
			data = businessDelegatorView.getDataMotSolSelect();
			RequestContext.getCurrentInstance().update("form:listaTDo");
		} catch (Exception e) {
			entity = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modify() {
		try {
			if (entity == null) {
				Long idMotSolSelected = new Long(
						selectedMotSolSelect.getIdMotSolSelected());
				entity = businessDelegatorView
						.getMotSolSelect(idMotSolSelected);
			}

			entity.setMotivoSolicitud((FacesUtils
					.checkLong(txtIdMotSol_MotivoSolicitud) != null) ? businessDelegatorView
					.getMotivoSolicitud(FacesUtils
							.checkLong(txtIdMotSol_MotivoSolicitud)) : null);
			entity.setSolicitudPqr((FacesUtils
					.checkLong(txtIdSolPqr_SolicitudPqr) != null) ? businessDelegatorView
					.getSolicitudPqr(FacesUtils
							.checkLong(txtIdSolPqr_SolicitudPqr)) : null);
			businessDelegatorView.updateMotSolSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			data = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_datatable(ActionEvent evt) {
		try {
			selectedMotSolSelect = (MotSolSelectDTO) (evt.getComponent()
					.getAttributes().get("selectedMotSolSelect"));

			Long idMotSolSelected = new Long(
					selectedMotSolSelect.getIdMotSolSelected());
			entity = businessDelegatorView.getMotSolSelect(idMotSolSelected);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_master() {
		try {
			Long idMotSolSelected = FacesUtils.checkLong(txtIdMotSolSelected);
			entity = businessDelegatorView.getMotSolSelect(idMotSolSelected);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public void action_delete() throws Exception {
		try {
			businessDelegatorView.deleteMotSolSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data = null;
		} catch (Exception e) {
			throw e;
		}
	}

	public String action_closeDialog() {
		setShowDialog(false);
		action_clear();

		return "";
	}

	public String actionDeleteDataTableEditable(ActionEvent evt) {
		try {
			selectedMotSolSelect = (MotSolSelectDTO) (evt.getComponent()
					.getAttributes().get("selectedMotSolSelect"));

			Long idMotSolSelected = new Long(
					selectedMotSolSelect.getIdMotSolSelected());
			entity = businessDelegatorView.getMotSolSelect(idMotSolSelected);
			businessDelegatorView.deleteMotSolSelect(entity);
			data.remove(selectedMotSolSelect);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modifyWitDTO(Long idMotSolSelected,
			Long idMotSol_MotivoSolicitud, Long idSolPqr_SolicitudPqr)
			throws Exception {
		try {
			businessDelegatorView.updateMotSolSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("MotSolSelectView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}

	public InputText getTxtIdMotSol_MotivoSolicitud() {
		return txtIdMotSol_MotivoSolicitud;
	}

	public void setTxtIdMotSol_MotivoSolicitud(
			InputText txtIdMotSol_MotivoSolicitud) {
		this.txtIdMotSol_MotivoSolicitud = txtIdMotSol_MotivoSolicitud;
	}

	public InputText getTxtIdSolPqr_SolicitudPqr() {
		return txtIdSolPqr_SolicitudPqr;
	}

	public void setTxtIdSolPqr_SolicitudPqr(InputText txtIdSolPqr_SolicitudPqr) {
		this.txtIdSolPqr_SolicitudPqr = txtIdSolPqr_SolicitudPqr;
	}

	public InputText getTxtIdMotSolSelected() {
		return txtIdMotSolSelected;
	}

	public void setTxtIdMotSolSelected(InputText txtIdMotSolSelected) {
		this.txtIdMotSolSelected = txtIdMotSolSelected;
	}

	public List<MotSolSelectDTO> getData() {
		try {
			if (data == null) {
				data = businessDelegatorView.getDataMotSolSelect();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<MotSolSelectDTO> motSolSelectDTO) {
		this.data = motSolSelectDTO;
	}

	public MotSolSelectDTO getSelectedMotSolSelect() {
		return selectedMotSolSelect;
	}

	public void setSelectedMotSolSelect(MotSolSelectDTO motSolSelect) {
		this.selectedMotSolSelect = motSolSelect;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public IBusinessDelegatorView getBusinessDelegatorView() {
		return businessDelegatorView;
	}

	public void setBusinessDelegatorView(
			IBusinessDelegatorView businessDelegatorView) {
		this.businessDelegatorView = businessDelegatorView;
	}

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	public void onEdit(org.primefaces.event.RowEditEvent event) {

		try {

			MotSolSelectDTO m = (MotSolSelectDTO) event.getObject();

			entity = null;
			entity = businessDelegatorView.getMotSolSelect(m
					.getIdMotSolSelected());

			motivoSolicitud = m.getIdMotSol_MotivoSolicitud();
			solicitudPqr = m.getIdSolPqr_SolicitudPqr();

			MotivoSolicitud motivoSolicitudItem = businessDelegatorView
					.getMotivoSolicitud(motivoSolicitud);

			SolicitudPqr solicitudPqrItem = businessDelegatorView
					.getSolicitudPqr(solicitudPqr);

			entity.setSolicitudPqr(solicitudPqrItem);
			entity.setMotivoSolicitud(motivoSolicitudItem);

			businessDelegatorView.updateMotSolSelect(entity);

			data = businessDelegatorView.getDataMotSolSelect();

			RequestContext.getCurrentInstance().update("form:listaTDo");

			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void onCancel(org.primefaces.event.RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Cancelado",
				((MotReclSelectDTO) event.getObject()).getIdMotReclSelect()
						+ "");

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public Long getMotivoSolicitud() {
		return motivoSolicitud;
	}

	public void setMotivoSolicitud(Long motivoSolicitud) {
		this.motivoSolicitud = motivoSolicitud;
	}

	public Long getSolicitudPqr() {
		return solicitudPqr;
	}

	public void setSolicitudPqr(Long solicitudPqr) {
		this.solicitudPqr = solicitudPqr;
	}

	public SelectOneMenu getTxtSolicitudPqr() {
		return txtSolicitudPqr;
	}

	public void setTxtSolicitudPqr(SelectOneMenu txtSolicitudPqr) {
		this.txtSolicitudPqr = txtSolicitudPqr;
	}

	public SelectOneMenu getTxtMotivoSolicitud() {
		return txtMotivoSolicitud;
	}

	public void setTxtMotivoSolicitud(SelectOneMenu txtMotivoSolicitud) {
		this.txtMotivoSolicitud = txtMotivoSolicitud;
	}

	public Map<String, String> getLosItemsSolicitud() {
		try {
			if (data3 == null) {
				getData3();
				for (int i = 0; i < data3.size(); i++) {
					losItemsSolicitud.put(data3.get(i).getNumeroRadicacion(),
							data3.get(i).getIdSolPqr() + "");

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsSolicitud;
	}

	public void setLosItemsSolicitud(Map<String, String> losItemsSolicitud) {
		this.losItemsSolicitud = losItemsSolicitud;
	}

	public Map<String, String> getLosItemsMotivoSolicitud() {
		try {
			if (data2 == null) {
				getData2();
				for (int i = 0; i < data2.size(); i++) {
					losItemsMotivoSolicitud.put(data2.get(i)
							.getDescripcionMotSol(), data2.get(i).getIdMotSol()
							+ "");

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsMotivoSolicitud;
	}

	public void setLosItemsMotivoSolicitud(
			Map<String, String> losItemsMotivoSolicitud) {
		this.losItemsMotivoSolicitud = losItemsMotivoSolicitud;
	}

	public CommandButton getBtnBack() {
		return btnBack;
	}

	public void setBtnBack(CommandButton btnBack) {
		this.btnBack = btnBack;
	}

	public List<MotivoSolicitudDTO> getData2() {
		try {
			if (data2 == null) {
				data2 = businessDelegatorView.getDataMotivoSolicitud();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data2;
	}

	public void setData2(List<MotivoSolicitudDTO> data2) {
		this.data2 = data2;
	}

	public List<SolicitudPqrDTO> getData3() {
		try {
			if (data3 == null) {
				data3 = businessDelegatorView.getDataSolicitudPqr();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data3;
	}

	public void setData3(List<SolicitudPqrDTO> data3) {
		this.data3 = data3;
	}

	public MotSolSelect getEntity() {
		return entity;
	}

	public void setEntity(MotSolSelect entity) {
		this.entity = entity;
	}

}
